package com.platform.api;

import cn.hutool.core.lang.Pair;
import com.platform.constant.ApiConstant;
import com.platform.dao.ApiOrderGoodsMapper;
import com.platform.entity.*;
import com.platform.service.*;
import com.platform.util.ApiBaseAction;
import com.platform.util.ApiUserUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分润逻辑（系统改造后）
 * @Author jaalemon@163.com
 * @Date 2022/11/23 15:56
 * @Version 1.0
 */
@Slf4j
@Component
public class ApiFxController extends ApiBaseAction {

    @Autowired
    private ApiOrderGoodsMapper apiOrderGoodsMapper;
    @Autowired
    private ApiOrderService orderService;
    @Autowired
    private ApiUserService userService;
    @Autowired
    private MlsUserSer mlsUserSer;
    @Autowired
    private ApiUserCouponService userCouponService;
    @Autowired
    private ApiGoodsService goodsService;
    @Autowired
    private UserRecordSer userRecordSer;

    /**
     * 计算分润V2版本
     * @param userId 用户id
     * @param orderPrice 订单价格
     * @param orderId 订单id
     * @return
     */
    public Object fxV2(Long userId, BigDecimal orderPrice, Integer orderId) {
        // 当前购买商品的用户
        Pair<UserVo, MlsUserEntity2> curUser = this.getUserObject(userId);
        if(curUser == null){
            return toResponsFail("不存在该会员,userId" + userId);
        }

        // 获取订单信息
        Map<String, Object> params = new HashMap<>();
        params.put("order_id", orderId);
        List<OrderGoodsVo> orderGoodsVoList = apiOrderGoodsMapper.queryList(params);
        if (CollectionUtils.isEmpty(orderGoodsVoList)) {
            return toResponsFail("无效订单id,orderId" + orderId);
        }

        log.warn("fx==>"+curUser.getKey().getNickname() +"购买商品，级别："+ ApiUserUtils.getLevelName(curUser.getKey().getUser_level_id())+"，订单号："+orderId+"，订单金额："+orderPrice);
        log.warn("fx==>开始...");

        if (ApiConstant.RETAIL_USER == curUser.getKey().getUser_level_id()) {// 游客购买
            log.warn("fx==>游客购买，判断是否要升级VIP!");
            // 获取游客历史的订单
            Map<String, Object> orderParams = new HashMap<>();
            orderParams.put("user_id", curUser.getKey().getUserId());
            List<OrderVo> orderVoList = orderService.queryList(orderParams);
            BigDecimal dbAllPrice = orderVoList.stream().map(OrderVo::getOrder_price).reduce(BigDecimal.ZERO, BigDecimal::add);
            int allPrice = dbAllPrice.intValue();
            log.warn("fx==>游客累计消费金额："+allPrice);
            if(allPrice >= 990){
                // 升级成VIP
                log.warn("fx==>"+curUser.getKey().getNickname()+"升级成VIP");
                UserVo userVo = new UserVo();
                userVo.setUserId(curUser.getKey().getUserId());
                userVo.setUser_level_id(ApiConstant.VIP_USER);
                userService.update(userVo);
            }
        }

        // 当前用户的推荐人
        Pair<UserVo, MlsUserEntity2> promoterOfCurUser = this.getUserObject(curUser.getKey().getPromoterId());
        if(promoterOfCurUser != null){
            log.warn("fx==>"+curUser.getKey().getNickname()+"的推荐人是："+promoterOfCurUser.getKey().getNickname()+"，推荐人等级："+ApiUserUtils.getLevelName(promoterOfCurUser.getKey().getUser_level_id()));
        }

        // 当前用户的上级
        Pair<UserVo, MlsUserEntity2> fidOfCurUser = this.getFidUserObject(curUser.getValue().getFid());

        // 该笔订单使用的活动现金券
        Map<String, Object> couponParams = new HashMap<>();
        couponParams.put("order_id", orderId);
        couponParams.put("coupon_status", 2);
        couponParams.put("coupon_id", 30); // 活动现金券
        List<UserCouponVo>  userCouponVoList = userCouponService.queryList(couponParams);
        // 该笔订单使用了活动现金券，不需要分销
        if(CollectionUtils.isNotEmpty(userCouponVoList) && userCouponVoList.size() > 0){
            log.warn("fx==>该笔订单使用了活动现金券，不需要分销orderId="+orderId+"，活动现金券个数="+userCouponVoList.size());
        }else{
            // 计算每个产品的分润
            for (OrderGoodsVo orderGoodsVo : orderGoodsVoList) {
                // 获取商品详情
                GoodsVo goodsVo = goodsService.queryObject(orderGoodsVo.getGoods_id());
                // 非活动商品才参与分销
                if(!goodsVo.getIs_secKill().equals(5)){
                    log.warn("fx==>分销的商品："+goodsVo.getName()+"，购买数量："+orderGoodsVo.getNumber());
                    if (ApiConstant.RETAIL_USER == curUser.getKey().getUser_level_id()) {// 游客首次购买
                        log.warn("fx==>游客首次购买!");
                        retailBuyFx(orderPrice, orderId, curUser, promoterOfCurUser, fidOfCurUser, orderGoodsVo, goodsVo);
                    } else if (ApiConstant.VIP_USER == curUser.getKey().getUser_level_id()) {// VIP复购
                        log.warn("fx==>VIP复购，计算分润和游客一样!");
                        retailBuyFx(orderPrice, orderId, curUser, promoterOfCurUser, fidOfCurUser, orderGoodsVo, goodsVo);
                    } else {// 代理购买
                        log.warn("fx==>代理购买!");
                        proxyBuyFx(orderPrice, orderId, curUser, promoterOfCurUser, fidOfCurUser, orderGoodsVo, goodsVo);
                    }
                }else{
                    log.warn("fx==>该商品是活动商品，不参与分润："+goodsVo.getName()+"，购买数量："+orderGoodsVo.getNumber());
                }
            }
        }
        log.warn("fx==>结束！！！");
        return toResponsSuccess("分润完成");
    }

    /**
     * 游客购买分润
     * @param orderPrice 订单价格
     * @param orderId 订单id
     * @param curUser 当前购买用户
     * @param promoterOfcurUser 购买用户的推荐人
     * @param fidOfcurUser 购买用户的上级
     * @param orderGoodsVo 订单关联的商品明细
     * @param goodsVo 商品信息
     */
    private void retailBuyFx(BigDecimal orderPrice, Integer orderId, Pair<UserVo, MlsUserEntity2> curUser, Pair<UserVo, MlsUserEntity2> promoterOfcurUser, Pair<UserVo, MlsUserEntity2> fidOfcurUser, OrderGoodsVo orderGoodsVo, GoodsVo goodsVo) {
        String title = ApiUserUtils.getLevelName(curUser.getKey().getUser_level_id());
        // 是否要给vip推荐人分润, 现金券
        boolean isVipFx = false, isVipCouponFx = false;
        // 计算给VIP的分润
        BigDecimal vipPrice = BigDecimal.ZERO;
        // 现金券金额
        BigDecimal couponAmount = BigDecimal.ZERO;
        // 同级培育奖 = round(游客零售价 * 0.03) * 数量
        BigDecimal levelPrice = goodsVo.getRetail_price().multiply(new BigDecimal(0.03)).setScale(0, BigDecimal.ROUND_HALF_UP)
                .multiply(new BigDecimal(orderGoodsVo.getNumber()));

        // 1. 游客首次购买，他的推荐人是VIP, 没有分润，也没有同级培训奖，也没有现金券
        if(fidOfcurUser != null && fidOfcurUser.getKey().getUser_level_id() > 2){//上级有代理身份，进行分润
            if (ApiConstant.SHOP_USER.equals(fidOfcurUser.getKey().getUser_level_id())) { // 门店
                log.warn("fx==>"+title+"的直接上级是合伙人："+fidOfcurUser.getKey().getNickname());
                this.shopFx(orderPrice, orderId, curUser, curUser, fidOfcurUser, orderGoodsVo, goodsVo, isVipFx, isVipCouponFx, vipPrice, couponAmount, levelPrice);
            } else if (ApiConstant.AGENT_USER.equals(fidOfcurUser.getKey().getUser_level_id())) {// 总代
                log.warn("fx==>"+title+"没有直接上级，他的上级是大区合伙人："+fidOfcurUser.getKey().getNickname());
                this.agentFx(orderPrice, orderId, curUser, curUser, fidOfcurUser, orderGoodsVo, goodsVo, isVipFx, isVipCouponFx, false, vipPrice, couponAmount, levelPrice);
            } else if (ApiConstant.REGION_USER.equals(fidOfcurUser.getKey().getUser_level_id())) {// 大区
                log.warn("fx==>"+title+"没有直接上级，他的上级是联合创始人："+fidOfcurUser.getKey().getNickname());
                this.regionFx(orderPrice, orderId, curUser, curUser, fidOfcurUser, orderGoodsVo, goodsVo, isVipFx, isVipCouponFx, false, vipPrice, couponAmount, levelPrice);
            }else if (ApiConstant.COOP_USER.equals(fidOfcurUser.getKey().getUser_level_id())) {// 战略
                log.warn("fx==>"+title+"没有直接上级，他的上级是战略合作商："+fidOfcurUser.getKey().getNickname());
                this.coopFx(orderPrice, orderId, curUser, curUser, fidOfcurUser, orderGoodsVo, goodsVo, isVipFx, isVipCouponFx, false, vipPrice, couponAmount, levelPrice);
            }
        }

    }

    /**
     * 代理购买分润
     * @param orderPrice
     * @param orderId
     * @param curUser
     * @param promoterOfcurUser
     * @param fidOfcurUser
     * @param orderGoodsVo
     * @param goodsVo
     */
    private void proxyBuyFx(BigDecimal orderPrice, Integer orderId, Pair<UserVo, MlsUserEntity2> curUser, Pair<UserVo, MlsUserEntity2> promoterOfcurUser, Pair<UserVo, MlsUserEntity2> fidOfcurUser, OrderGoodsVo orderGoodsVo, GoodsVo goodsVo) {
        log.warn("fx==>代理购买，先给他的推荐人分润");
        // 同级培育奖 = round(游客零售价 * 0.03) * 数量
        BigDecimal levelPrice = goodsVo.getRetail_price().multiply(new BigDecimal(0.03)).setScale(0, BigDecimal.ROUND_HALF_UP)
                .multiply(new BigDecimal(orderGoodsVo.getNumber()));

        // 给当前代理用户的推荐人分润， 是否分润成功
        boolean isPromoterFx = this.forProxyPromoterFx(orderPrice, orderId, curUser, curUser, levelPrice);

        if(fidOfcurUser != null){
            if (ApiConstant.AGENT_USER.equals(fidOfcurUser.getKey().getUser_level_id())) {// 总代
                log.warn("fx==>该代理的直接上级是大区合伙人："+fidOfcurUser.getKey().getNickname());
                this.agentFx(orderPrice, orderId, curUser, curUser, fidOfcurUser, orderGoodsVo, goodsVo, false, false, isPromoterFx, null, null, levelPrice);
            } else if (ApiConstant.REGION_USER.equals(fidOfcurUser.getKey().getUser_level_id())) {// 大区
                log.warn("fx==>该代理没有直接上级，他的上级是联合创始人："+fidOfcurUser.getKey().getNickname());
                this.regionFx(orderPrice, orderId, curUser, curUser, fidOfcurUser, orderGoodsVo, goodsVo, false, false, isPromoterFx, null, null, levelPrice);
            }else if (ApiConstant.COOP_USER.equals(fidOfcurUser.getKey().getUser_level_id())) {// 战略
                log.warn("fx==>该代理没有直接上级，他的上级是分公司："+fidOfcurUser.getKey().getNickname());
                this.coopFx(orderPrice, orderId, curUser, curUser, fidOfcurUser, orderGoodsVo, goodsVo, false, false, isPromoterFx, null, null, levelPrice);
            }
        }else{
            log.warn("fx==>该代理是最高级别战略，不用向上级分润");
        }
    }

    /**
     * 分公司分润
     * @param orderPrice
     * @param orderId
     * @param preUser
     * @param curUser
     * @param proxyUser
     * @param orderGoodsVo
     * @param goodsVo
     * @param isVipFx
     * @param isVipCouponFx
     * @param isPromoterFx
     * @param vipPrice
     * @param couponAmount
     * @param levelPrice
     */
    private void coopFx(BigDecimal orderPrice, Integer orderId, Pair<UserVo, MlsUserEntity2> preUser, Pair<UserVo, MlsUserEntity2> curUser, Pair<UserVo, MlsUserEntity2> proxyUser, OrderGoodsVo orderGoodsVo, GoodsVo goodsVo, boolean isVipFx, boolean isVipCouponFx, boolean isPromoterFx, BigDecimal vipPrice, BigDecimal couponAmount, BigDecimal levelPrice) {
        String title = ApiUserUtils.getLevelName(curUser.getKey().getUser_level_id());
        log.warn("fx==>给"+title+proxyUser.getKey().getNickname()+"分润...");

        // 默认游客和VIP零售价购买
        BigDecimal startPrice = goodsVo.getRetail_price();
        if(ApiConstant.SHOP_USER.equals(preUser.getKey().getUser_level_id())){
            startPrice = goodsVo.getShop_price();
        }else if(ApiConstant.AGENT_USER.equals(preUser.getKey().getUser_level_id())){
            startPrice = goodsVo.getAgent_price();
        }else if(ApiConstant.REGION_USER.equals(preUser.getKey().getUser_level_id())){
            startPrice = goodsVo.getRegion_price();
        }

        // 给战略分润需要加上自己的拿货成本
        BigDecimal coopPrice = startPrice
                .subtract(goodsVo.getCoop_price())
                .add(goodsVo.getCoop_price())
                .multiply(new BigDecimal(orderGoodsVo.getNumber()))
                .subtract(isPromoterFx ? levelPrice : BigDecimal.ZERO)
                .subtract(isVipFx ? vipPrice : BigDecimal.ZERO)
                .subtract(isVipCouponFx ? couponAmount : BigDecimal.ZERO);
        addUserRecord(proxyUser.getValue().getMlsUserId(), coopPrice.intValue(), curUser.getValue().getMlsUserId(), orderPrice.intValue(), orderId, "给"+title+"分润");
    }

    /**
     * 联合创始人分润
     * @param orderPrice
     * @param orderId
     * @param preUser
     * @param curUser
     * @param proxyUser
     * @param orderGoodsVo
     * @param goodsVo
     * @param isVipFx
     * @param isVipCouponFx
     * @param isPromoterFx
     * @param vipPrice
     * @param couponAmount
     * @param levelPrice
     */
    private void regionFx(BigDecimal orderPrice, Integer orderId, Pair<UserVo, MlsUserEntity2> preUser, Pair<UserVo, MlsUserEntity2> curUser, Pair<UserVo, MlsUserEntity2> proxyUser, OrderGoodsVo orderGoodsVo, GoodsVo goodsVo, boolean isVipFx, boolean isVipCouponFx, boolean isPromoterFx, BigDecimal vipPrice, BigDecimal couponAmount, BigDecimal levelPrice) {
        String title = ApiUserUtils.getLevelName(curUser.getKey().getUser_level_id());
        log.warn("fx==>从"+title+proxyUser.getKey().getNickname()+"开始分润...");

        // 默认游客和VIP零售价购买
        BigDecimal startPrice = goodsVo.getRetail_price();
        if(ApiConstant.SHOP_USER.equals(preUser.getKey().getUser_level_id())){
            startPrice = goodsVo.getShop_price();
        }else if(ApiConstant.AGENT_USER.equals(preUser.getKey().getUser_level_id())){
            startPrice = goodsVo.getAgent_price();
        }

        // 给联合创始人分润
        BigDecimal regionPrice = startPrice
                .subtract(goodsVo.getRegion_price())
                .multiply(new BigDecimal(orderGoodsVo.getNumber()))
                .subtract(isPromoterFx ? levelPrice : BigDecimal.ZERO)
                .subtract(isVipFx ? vipPrice : BigDecimal.ZERO)
                .subtract(isVipCouponFx ? couponAmount : BigDecimal.ZERO);
        addUserRecord(proxyUser.getValue().getMlsUserId(), regionPrice.intValue(), curUser.getValue().getMlsUserId(), orderPrice.intValue(), orderId, "for region");

        // 不需要给联合创始人的推荐人同级培训
        isPromoterFx = false;

        // 代理用户的上级
        Pair<UserVo, MlsUserEntity2> fidOfProxyUser = this.getFidUserObject(proxyUser.getValue().getFid());

        // 只可能是分公司
        if(fidOfProxyUser != null && ApiConstant.COOP_USER.equals(fidOfProxyUser.getKey().getUser_level_id())){
            log.warn("fx==>"+title+"："+proxyUser.getKey().getNickname()+"的直接上级是分公司："+fidOfProxyUser.getKey().getNickname());
            this.coopFx(orderPrice, orderId, proxyUser, curUser, fidOfProxyUser, orderGoodsVo, goodsVo, false, false, isPromoterFx, null, null, levelPrice);

        }
    }

    /**
     * 大区合伙人分润
     * @param orderPrice
     * @param orderId
     * @param preUser
     * @param curUser
     * @param proxyUser
     * @param orderGoodsVo
     * @param goodsVo
     * @param isVipFx
     * @param isVipCouponFx
     * @param isPromoterFx
     * @param vipPrice
     * @param couponAmount
     * @param levelPrice
     */
    private void agentFx(BigDecimal orderPrice, Integer orderId, Pair<UserVo, MlsUserEntity2> preUser, Pair<UserVo, MlsUserEntity2> curUser, Pair<UserVo, MlsUserEntity2> proxyUser, OrderGoodsVo orderGoodsVo, GoodsVo goodsVo, boolean isVipFx, boolean isVipCouponFx, boolean isPromoterFx, BigDecimal vipPrice, BigDecimal couponAmount, BigDecimal levelPrice) {
        String title = ApiUserUtils.getLevelName(curUser.getKey().getUser_level_id());
        log.warn("fx==>从"+title+proxyUser.getKey().getNickname()+"开始分润...");

        // 默认游客和VIP零售价购买
        BigDecimal startPrice = goodsVo.getRetail_price();
        if(ApiConstant.SHOP_USER.equals(preUser.getKey().getUser_level_id())){
            startPrice = goodsVo.getShop_price();
        }

        // 给合伙人分润
        BigDecimal agentPrice = startPrice
                .subtract(goodsVo.getAgent_price())
                .multiply(new BigDecimal(orderGoodsVo.getNumber()))
                .subtract(isPromoterFx ? levelPrice : BigDecimal.ZERO)
                .subtract(isVipFx ? vipPrice : BigDecimal.ZERO)
                .subtract(isVipCouponFx ? couponAmount : BigDecimal.ZERO);
        addUserRecord(proxyUser.getValue().getMlsUserId(), agentPrice.intValue(), curUser.getValue().getMlsUserId(), orderPrice.intValue(), orderId, "给"+title+"的分润");

        // 给合伙人推荐人， 是否分润成功
        isPromoterFx = this.forProxyPromoterFx(orderPrice, orderId, proxyUser, curUser, levelPrice);

        // 代理用户的上级
        Pair<UserVo, MlsUserEntity2> fidOfProxyUser = this.getFidUserObject(proxyUser.getValue().getFid());

        if(fidOfProxyUser != null){
            if (ApiConstant.REGION_USER.equals(fidOfProxyUser.getKey().getUser_level_id())) {// 联合创始人
                log.warn("fx==>"+title+"："+proxyUser.getKey().getNickname()+"的直接上级是联合创始人："+fidOfProxyUser.getKey().getNickname());
                this.regionFx(orderPrice, orderId, proxyUser, curUser, fidOfProxyUser, orderGoodsVo, goodsVo, false, false, isPromoterFx, null, null, levelPrice);

            }else if(ApiConstant.COOP_USER.equals(fidOfProxyUser.getKey().getUser_level_id())){// 分公司
                log.warn("fx==>"+title+"："+proxyUser.getKey().getNickname()+"没有直接上级， 他的上级是分公司："+fidOfProxyUser.getKey().getNickname());
                this.coopFx(orderPrice, orderId, proxyUser, curUser, fidOfProxyUser, orderGoodsVo, goodsVo, false, false, isPromoterFx, null, null, levelPrice);
            }
        }
    }

    /**
     * 合伙人分润
     * @param orderPrice
     * @param orderId
     * @param preUser
     * @param curUser
     * @param proxyUser
     * @param orderGoodsVo
     * @param goodsVo
     * @param isVipFx
     * @param isVipCouponFx
     * @param vipPrice
     * @param couponAmount
     * @param levelPrice
     */
    private void shopFx(BigDecimal orderPrice, Integer orderId, Pair<UserVo, MlsUserEntity2> preUser, Pair<UserVo, MlsUserEntity2> curUser, Pair<UserVo, MlsUserEntity2> proxyUser, OrderGoodsVo orderGoodsVo, GoodsVo goodsVo, boolean isVipFx, boolean isVipCouponFx, BigDecimal vipPrice, BigDecimal couponAmount, BigDecimal levelPrice) {
        String title = ApiUserUtils.getLevelName(curUser.getKey().getUser_level_id());
        log.warn("fx==>从"+title+proxyUser.getKey().getNickname()+"开始分润...");
        // 游客和VIP都是零售价购买
        BigDecimal startPrice = goodsVo.getRetail_price();

        // 给合伙人分润
        BigDecimal shopPrice = startPrice
                .subtract(goodsVo.getShop_price())
                .multiply(new BigDecimal(orderGoodsVo.getNumber()))
                .subtract(isVipFx ? vipPrice : BigDecimal.ZERO)
                .subtract(isVipCouponFx ? couponAmount : BigDecimal.ZERO);
        addUserRecord(proxyUser.getValue().getMlsUserId(), shopPrice.intValue(), curUser.getValue().getMlsUserId(), orderPrice.intValue(), orderId, "给"+title+"的分润");

        // 给合伙人推荐人， 是否分润成功
        boolean isPromoterFx = this.forProxyPromoterFx(orderPrice, orderId, proxyUser, curUser, levelPrice);

        // 代理用户的上级
        Pair<UserVo, MlsUserEntity2> fidOfProxyUser = this.getFidUserObject(proxyUser.getValue().getFid());

        if(fidOfProxyUser != null){
            if (ApiConstant.AGENT_USER.equals(fidOfProxyUser.getKey().getUser_level_id())) {// 总代
                log.warn("fx==>"+title+"："+proxyUser.getKey().getNickname()+"的直接上级是大区合伙人："+fidOfProxyUser.getKey().getNickname());
                this.agentFx(orderPrice, orderId, proxyUser, curUser, fidOfProxyUser, orderGoodsVo, goodsVo, false, false, isPromoterFx, null, null, levelPrice);
            }else if(ApiConstant.REGION_USER == fidOfProxyUser.getKey().getUser_level_id()){// 大区
                log.warn("fx==>"+title+"："+proxyUser.getKey().getNickname()+"没直接上级，上级是联合创始人："+fidOfProxyUser.getKey().getNickname());
                this.regionFx(orderPrice, orderId, proxyUser, curUser, fidOfProxyUser, orderGoodsVo, goodsVo, false, false, isPromoterFx, null, null, levelPrice);
            }else if(ApiConstant.COOP_USER == fidOfProxyUser.getKey().getUser_level_id()){// 战略
                log.warn("fx==>"+title+"："+proxyUser.getKey().getNickname()+"没有直接上级， 他的上级是分公司："+fidOfProxyUser.getKey().getNickname());
                this.coopFx(orderPrice, orderId, proxyUser, curUser, fidOfProxyUser, orderGoodsVo, goodsVo, false, false, isPromoterFx, null, null, levelPrice);
            }
        }
    }

    /**
     * 推荐人分润
     * @param orderPrice
     * @param orderId
     * @param curProxyUser
     * @param buyUser
     * @param levelPrice
     * @return
     */
    private boolean forProxyPromoterFx(BigDecimal orderPrice, Integer orderId, Pair<UserVo, MlsUserEntity2> curProxyUser, Pair<UserVo, MlsUserEntity2> buyUser, BigDecimal levelPrice){
        // 代理用户的推荐人
        Pair<UserVo, MlsUserEntity2> promoterOfCurProxyUser = this.getUserObject(curProxyUser.getKey().getPromoterId());
        if(promoterOfCurProxyUser != null){
            String promoterLevelName = ApiUserUtils.getLevelName(promoterOfCurProxyUser.getKey().getUser_level_id());
            log.warn("fx==>" + ApiUserUtils.getLevelName(curProxyUser.getKey().getUser_level_id()) + ":" + curProxyUser.getKey().getNickname()+"的推荐人是："+promoterOfCurProxyUser.getKey().getNickname()+"，推荐人等级："+ promoterLevelName);
            // 推荐人的级别和他一样，则获取同级培育奖
            if (promoterOfCurProxyUser.getKey().getUser_level_id() == curProxyUser.getKey().getUser_level_id()) {
                // 给推荐人分润
                addUserRecord(promoterOfCurProxyUser.getValue().getMlsUserId(), levelPrice.intValue(), buyUser.getValue().getMlsUserId(), orderPrice.intValue(), orderId, "给"+promoterLevelName+"的同级培育奖");
                return true;
            }
        }
        return false;
    }

    /**
     * 增加分润记录
     * @param mlsUserId
     * @param price
     */
    private void addUserRecord(Long mlsUserId, int price, Long fxuser, int order_price, int orderId, String remark) {
        String remarks = remark +":"+fxuser+"给:"+mlsUserId+"分润，金额："+price;
        log.warn("fx==>"+remarks);
        MlsUserEntity2 mlsUser = mlsUserSer.getEntityMapper().getById(mlsUserId);

        UserRecord ur = new UserRecord();
        ur.setMlsUserId(mlsUserId);
        ur.setTypes(2);
        ur.setTypesStr("分润");
        ur.setPrice(price);
        ur.setRemarks(remarks);
        ur.setOrderId(orderId);
        ur.setDirection(1);//收入
        ur.setAmount(new BigDecimal(mlsUser.getAllProfit() + price));
        userRecordSer.save(ur);

        MlsUserEntity2 mlsUserVo= new MlsUserEntity2();
        mlsUserVo.setMlsUserId(mlsUserId);
        mlsUserVo.setTodaySales(order_price);
        mlsUserVo.setGetProfit(price);
        mlsUserSer.updateMoney(mlsUserVo);
    }

    /**
     * 根据用户id获取用户对象
     * @param userId
     * @return
     */
    private Pair<UserVo, MlsUserEntity2> getUserObject(Long userId){
        UserVo userVo = null;
        MlsUserEntity2 fxUser = null;
        if (userId != null && userId > 0) {
            // 用户信息
            userVo = userService.queryObject(userId);
            if(userVo != null){
                // 对应的分销用户信息
                fxUser = mlsUserSer.getEntityMapper().findByUserId(userVo.getUserId());

                return new Pair<>(userVo, fxUser);
            }
        }
        return null;
    }

    /**
     * 根据分销用户的fid获取上级用户对象
     * @param fid
     * @return
     */
    private Pair<UserVo, MlsUserEntity2> getFidUserObject(Long fid){
        UserVo userVo = null;
        MlsUserEntity2 fxUser = null;
        if (fid != null && fid > 0) {
            fxUser = mlsUserSer.getEntityMapper().getById(fid);
            userVo = userService.queryObject(fxUser.getUserId());
            return new Pair<>(userVo, fxUser);
        }
        return null;
    }

}
